package icu.cqcai.xinsi.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 错误处理控制器
 */
@Controller
public class CustomErrorController implements ErrorController {

    @RequestMapping("/error")
    @ResponseBody
    public String handleError(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("jakarta.servlet.error.status_code");
        String errorMessage = (String) request.getAttribute("jakarta.servlet.error.message");
        String requestURI = (String) request.getAttribute("jakarta.servlet.error.request_uri");
        
        System.out.println("=== 错误处理器被调用 ===");
        System.out.println("状态码: " + statusCode);
        System.out.println("错误信息: " + errorMessage);
        System.out.println("请求URI: " + requestURI);
        
        return String.format(
            "<h1>错误 %d</h1><p>请求路径: %s</p><p>错误信息: %s</p><p>请检查应用配置</p>", 
            statusCode, requestURI, errorMessage
        );
    }
} 